package com.google.samples.apps.paging.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.samples.apps.library_common.extension.extension.loadImage
import com.google.samples.apps.paging.R
import com.google.samples.apps.paging.data.bean.GoodsBean

/**
 * Created by Administrator on 2021/7/10 0010.
 * Describe:
 */
class GoodsPagingAdapter : PagedListAdapter<GoodsBean, BaseViewHolder>(DIFF_CALLBACK) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        return BaseViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.layout_item_goods, parent, false)
        )
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val item = getItem(position)
        item?.let {
            holder.setText(R.id.tvName, item.pro_name)
                .setText(R.id.tvContent, item.pro_describe)
                .loadImage(R.id.image, item.pro_logo)
        }

    }

}

val DIFF_CALLBACK = object : DiffUtil.ItemCallback<GoodsBean>() {
    override fun areItemsTheSame(oldItem: GoodsBean, newItem: GoodsBean): Boolean {
        return oldItem.proprice_id == newItem.proprice_id
    }

    override fun areContentsTheSame(oldItem: GoodsBean, newItem: GoodsBean): Boolean {
        return oldItem.equals(newItem)
    }

}